package top.carrotguo.weibo.presenter;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.bean.UserInfo;
import top.carrotguo.weibo.contract.HomeContract;
import top.carrotguo.weibo.model.HomeModel;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;

import static top.carrotguo.weibo.ui.home.HomeFragment.LOAD_FIRST;
import static top.carrotguo.weibo.ui.home.HomeFragment.LOAD_MORE;
import static top.carrotguo.weibo.ui.home.HomeFragment.LOAD_REFRESH;

public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter {

    private HomeContract.Model mModel;

    public HomePresenter(){
        mModel = new HomeModel();
    }

    @Override
    public void getTimeline(Oauth2AccessToken token,int count, int page, int loadType) {
        mModel.getTimeline(token,count, page, new BaseCallback<HomeTimeLine, ErrorInfo>() {
            @Override
            public void onSuccessed(HomeTimeLine successInfo) {
                if (getView() != null) {
                    switch (loadType) {
                        case LOAD_FIRST : {
                            getView().showFirstLoadSuccess(successInfo);
                            break;
                        }
                        case LOAD_MORE : {
                            getView().showLoadmoreSuccess(successInfo);
                            break;
                        }
                        case LOAD_REFRESH : {
                            getView().showRefreshSuccess(successInfo);
                            break;
                        }
                    }
                }
            }

            @Override
            public void onError(ErrorInfo errorInfo) {
                if (getView() != null) {
                    switch (loadType) {
                        case LOAD_FIRST : {
                            getView().showFirstLoadFail(errorInfo);
                            break;
                        }
                        case LOAD_MORE : {
                            getView().showLoadmoreFail(errorInfo);
                            break;
                        }
                        case LOAD_REFRESH : {
                            getView().showRefreshFail(errorInfo);
                            break;
                        }
                    }
                }
            }
        });
    }

    @Override
    public void getUserInfo(Oauth2AccessToken token) {
        mModel.getUserUid(token.getToken(), new BaseCallback<String, ErrorInfo>() {
            @Override
            public void onSuccessed(String successInfo) {
                //再次请求获取用户详情
                reqUserInfo(token.getToken(),successInfo);
            }

            @Override
            public void onError(ErrorInfo errorInfo) {
                Logger.e("请求获取用户uid失败");
            }
        });
    }

    private void reqUserInfo(String token,String uid){
        mModel.getUserInfo(token, uid, new BaseCallback<UserInfo, ErrorInfo>() {
            @Override
            public void onSuccessed(UserInfo successInfo) {
                if (getView() != null) {
                    successInfo.setUid(uid);
                    getView().getUserInfoSuccessed(successInfo);
                }
            }

            @Override
            public void onError(ErrorInfo errorInfo) {
                Logger.e("获取用户详情失败");
            }
        });
    }

}
